package com.josework.springhelloworld.model.impl;

import com.josework.springhelloworld.model.Apple;
import com.josework.springhelloworld.model.ApplePredicate;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class AppleGreenColorPredicate implements ApplePredicate {

    public static void main(String[] args) {

        List<Apple> apples = new ArrayList<>();
        Apple a1 = Apple.builder().color("red").weight(152).build();
        Apple a2 = Apple.builder().color("green").weight(148).build();
        apples.add(a1);
        apples.add(a2);
        apples.forEach(
                item -> System.out.println(item.getColor())
        );
        List<Apple> res = filterApples(apples,new AppleGreenColorPredicate());
        System.out.println(res.size());
        res.forEach(
                item -> System.out.println(item.getColor())
        );
    }

    @Override
    public boolean test(Apple apple) {
        return "green".equals(apple.getColor());
    }

    public static List<Apple> filterApples(List<Apple> inventory, ApplePredicate p){

        List<Apple> result = new ArrayList<>();
        for (Apple apple: inventory) {
            if (p.test(apple)){     //谓词对象封装了测试苹果的条件
                result.add(apple);
            }
        }
        return result;

    }
}
